﻿CREATE FUNCTION [dbo].[ObfuscateAccountNumber]
(
	@AccountNumber nvarchar(40)
)
RETURNS nvarchar(40)
AS
BEGIN
	declare @mask nvarchar(40)
	set @mask = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
	declare @startPos int
	set @startPos = len(rtrim(@AccountNumber)) - 3
	if @startPos < 1
		set @startPos = 1
	RETURN substring(@mask, 1, @startPos - 1) + substring(@AccountNumber, @startPos, 41 - @startPos)
END